package com.cljz.makepassenger.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.model.net.bean.GetActivityUserinfoBean;
import com.cljz.makepassenger.present.activity.EditorInformationP;
import com.cljz.makepassenger.utils.CommonUtils;
import com.cljz.makepassenger.utils.T;
import com.cljz.makepassenger.utils.date.TextUtil;
import com.jakewharton.rxbinding.view.RxView;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by apple on 2017/5/8.
 */

public class EditorInformationActivity extends BaseActivity<EditorInformationP> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.fl_title_right)
    FrameLayout flTitleRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.et_information_linkman)
    EditText etInformationLinkman;
    @BindView(R.id.et_information_proof)
    EditText etInformationProof;
    @BindView(R.id.et_information_phone)
    EditText etInformationPhone;
    @BindView(R.id.et_information_email)
    EditText etInformationEmail;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.tv_overlook)
    TextView tvOverlook;
    private String activityId;

    @Override
    public void initData(Bundle savedInstanceState) {
        tvTitleName.setText("身份信息");
        activityId = getIntent().getExtras().getString("activity_id");
        getP().getActivityUserInfo();
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.pop((Activity) App.getInstance());
                    }
                });
        RxView.clicks(tvOverlook)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        getP().activityUserSet(activityId);
                    }
                });
        RxView.clicks(btnSubmit)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        String realName = etInformationLinkman.getText().toString().trim();
                        String idCard = etInformationProof.getText().toString().trim();
                        String phone = etInformationPhone.getText().toString().trim();
                        String email = etInformationEmail.getText().toString().trim();
                        if (!TextUtil.isEmpty(realName)) {
                            if (CommonUtils.checkName(realName)) {
                                if (!TextUtil.isEmpty(idCard)) {
                                    if (CommonUtils.checkIdCard(idCard)) {
                                        if (!TextUtil.isEmpty(phone)) {
                                            if (CommonUtils.checkPhone(phone)) {
                                                if (!TextUtil.isEmpty(email)) {
                                                    if (CommonUtils.checkEmail(email)) {
                                                        if (null != activityId) {
                                                            getP().activityUserSet(activityId, realName, idCard, phone, email);
                                                        } else {
                                                            T.getInstance().showShort("网络错误");
                                                        }
                                                    } else {
                                                        T.getInstance().showShort("邮箱错误");
                                                    }
                                                } else {
                                                    T.getInstance().showShort("邮箱不能为空");

                                                }
                                            } else {
                                                T.getInstance().showShort("联系电话错误");

                                            }
                                        } else {
                                            T.getInstance().showShort("联系电话不能为空");

                                        }
                                    } else {
                                        T.getInstance().showShort("身份证号错误");

                                    }
                                } else {
                                    T.getInstance().showShort("身份证号不能为空");

                                }
                            } else {
                                T.getInstance().showShort("联系人错误");

                            }
                        } else {
                            T.getInstance().showShort("联系人不能为空");
                        }
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_editor_information;
    }

    @Override
    public EditorInformationP newP() {
        return new EditorInformationP();
    }

    public void getActivityUserinfoSucceed(GetActivityUserinfoBean.DataBean data) {
        if (data.getReal_name() != null) {
            etInformationLinkman.setText(data.getReal_name());
        }
        if (data.getIdcard() != null) {
            etInformationProof.setText(data.getIdcard());
        }
        if (data.getPhone() != null) {
            etInformationPhone.setText(data.getPhone());
        }
        if (data.getReal_name() != null) {
            etInformationEmail.setText(data.getEmail());
        }
    }

    public void getActivityUserinfoFail(String msg) {

    }

    public void activityUserSetSucceed(String msg) {
        T.getInstance().showShort(msg);
        setResult(RESULT_OK);
        Router.pop((Activity) App.getInstance());
    }

    public void activityUserSetFail(String error) {
        T.getInstance().showShort(error);
    }
}
